package top.zsyle.sycc.pay.alipay;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

public class Main {

    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            //AlipayTradePrecreateResponse response = Payment.FaceToFace().preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
            AlipayTradePagePayResponse response = Payment.Page().pay("测试支付宝支付！", "21984673218947623642736", "1000.00", "http://127.0.0.1:6002/detail.html?courseId=22");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功" + response.getBody());
            } else {
                System.err.println("调用失败，原因：" + response.getBody() + "，" + response.toString());
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com";
        config.signType = "RSA2";
        config.appId = "2021000122605029";
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCk2j79rpUo1ujDvo+8zjgT3DJkIEmE9J1ydB7Baw58Vs48ROYM+o47+gyriMWVrfQd1wK6fCPjENtX2uvA09eIcjoJxsRBeD0ZXe/r61C7ClhkUbGWXtUfWPB6XxijadEn8OnXpBrs+RgYFw7pVaMh5mJ+UwlcoQ2eMIispZ5MbSXtYpP3YOzt/panIxgKj8vDEV5w82NMBC1d2zS2HuIpY0y6DQQAU+wsBAFQY1qffZ52D0h+/cGzNP5m0+k+Slf4wOjM+8TfP1e+NmB5U0PVMLxQL4zUHWPNnGdllzU2Xo6YT+/8g688S9dxC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bcCg70ffvwwkMI0LdUxyoGc7b5W9e4ophN77FyPPun2TXT/1qeh64otN3fhkpd9KdkZnw5hTjkHYFPKAJGu72Yz7VrAOpArSPms+JANDmbgGQBfXDn8RKWfdYgdsgGuuVK6pr4yLz3ghpC7cmTAIykr/FeK6x76TvbEEu";
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuj0bkhEVs7MxclLWpdxjLaEfdYlqdd9S6FkgcH6rDK5dk7Q4xTJ6TArLap7bbGMf4YxkJpgIy9s4J3NSC4GE0nS8mwAhsfG8zkhWb1h4dxy9hD4CtqDXTWFfI8syanyLqtCL3lPyyzVf2c4yS4y9rrRWnxqIkFwx3CP0yR2KoPk1AIyQ3nH4oYKFV+Jvqe/ckSAxt3Th+agjwXiyjG0G5NOqyKqcPkUyMnmrErPOatCGJPInl/1vpBrpmEaotn3E0tM3bW3oecdZHjP0hYHdagcO5rHbEriYhnbfQdyI1FYp/NQh6IefTWxqy9btqD9vp+I8Q5DcYiEQ58Gl54u+D8GnawHHb4pMhF2FwGrFFs28yf8VqZWdb7hPsVauStqWFz+RUf/oyTnJ3bP+cOKpIHR6mvhHy84YzqjWRwRo/Si0Zql6VCFOSse2DBTPoxwwzJDhp7UF/n8BHWZChveGAwV/iLKJFAl/acUaeXNXpVBWcfG4UYZ4TBjTWzDVuQIDAQAB";
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "www.baidu.com";
        return config;
    }

}